//
//  LeftViewController.m
//  cityStar
//
//  Created by wangjia on 14/12/13.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "LeftViewController.h"
#import "SideViewController.h"
#import "HTDefine.h"
#import "PersonalDetail.h"
#import "UIColor+FlatUI.h"
#import "LoginCityStarViewViewController.h"
#import "BaseNavController.h"
#import "UIButton+WebCache.h"
@interface LeftViewController (){
    SideViewController *_vcSide;
}

@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.ivName.layer.cornerRadius = 8;
    self.ivName.layer.masksToBounds = YES;
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    NSData *imageData = [defaults dataForKey:@"defaultImage"];
    UIImage *backGroundImage = [UIImage imageWithData:imageData];
    if (backGroundImage) {
        self.tableView.backgroundColor =[UIColor colorWithPatternImage:backGroundImage];
    }else{
        self.tableView.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"xingkong"]];
    }
    [defaults synchronize];
    
    _vcSide = [SideViewController shareVc];//单例

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeBackGroundImage) name:@"changeBackGroundImage" object:nil];
//    self.lblName.text =
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginSuccessRefreshLeftView) name:@"LoginCityStarViewSuccess" object:nil];
    self.firstCell.selected = YES;
    self.firstCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.firstCell.frame];
    self.firstCell.selectedBackgroundView.backgroundColor=[UIColor colorWithRed:0.220 green:0.227 blue:0.255 alpha:0.600];
    
    [self loginSuccessRefreshLeftView];
}

-(void)loginSuccessRefreshLeftView
{
    NSData *userData = [KStandUserDefaults objectForKey:KUserDefaultKey_PersonalDetail];
    PersonalDetail *user = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
    if (user.nickname) {
        self.isLogin = YES;
        self.lblName.text = user.nickname;
        [self.ivName sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.icszx.com/image/c/id/%@/w/160/h/155/m/0",user.avatar]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"头像@"]];
    }else{
        self.isLogin = NO;
        self.lblName.text = @"请登录";
        [self.ivName setImage:[UIImage imageNamed:@"头像@"] forState:UIControlStateNormal];
    }
}

-(void)changeBackGroundImage
{
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    NSData *imageData = [defaults dataForKey:@"defaultImage"];
    UIImage *backGroundImage = [UIImage imageWithData:imageData];
    if (backGroundImage) {
        self.tableView.backgroundColor =[UIColor colorWithPatternImage:backGroundImage];
    }else{
        self.tableView.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"xingkong"]];
    }
    [defaults synchronize];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)btnAction:(id)sender {
    
    if (self.isLogin == NO) {
        [self presentLoginViewController];
        return;
    }
    [_vcSide showContentViewWithModel:@"PeopleCenter" withSb:@"SideLeft"];
}

-(void)celeanBackGroundViewWithtag:(int)tag
{
    if (tag != 1) {
        self.firstCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.firstCell.frame];
        self.firstCell.selectedBackgroundView.backgroundColor=[UIColor clearColor];
    }
    if (tag != 2){
        self.secondCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.secondCell.frame];
        self.secondCell.selectedBackgroundView.backgroundColor=[UIColor clearColor];
    }
    if (tag != 3){
        self.thirdCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.thirdCell.frame];
        self.thirdCell.selectedBackgroundView.backgroundColor=[UIColor clearColor];
    }
    if (tag != 4){
        self.forthCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.forthCell.frame];
        self.forthCell.selectedBackgroundView.backgroundColor=[UIColor clearColor];
    }
    if (tag != 5){
        self.fivethCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.fivethCell.frame];
        self.fivethCell.selectedBackgroundView.backgroundColor=[UIColor clearColor];
    }
    if (tag != 6){
        self.sixthCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.sixthCell.frame];
        self.sixthCell.selectedBackgroundView.backgroundColor=[UIColor clearColor];
    }
    
}

#pragma mark - UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [self.tableView reloadData];

    switch (indexPath.row) {
        case 0:
        {
            self.firstCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.firstCell.frame];
            self.firstCell.selectedBackgroundView.backgroundColor=[UIColor colorWithRed:0.220 green:0.227 blue:0.255 alpha:0.600];
            [self celeanBackGroundViewWithtag:1];
            if(_vcSide)
            {
                [_vcSide showContentViewWithModel:@"SIMain" withSb:@"Main"];
            }
        }
            break;
        case 1:
        {
            if (self.isLogin == NO) {
                [self presentLoginViewController];
                return;
            }
            self.secondCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.secondCell.frame];
            self.secondCell.selectedBackgroundView.backgroundColor=[UIColor colorWithRed:0.220 green:0.227 blue:0.255 alpha:0.600];
            [self celeanBackGroundViewWithtag:2];
            if(_vcSide)
            {
                [_vcSide showContentViewWithModel:@"BuyCart" withSb:@"SideLeft"];
            }
            
        }
            break;

        case 2:
        {
            self.thirdCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.thirdCell.frame];
            self.thirdCell.selectedBackgroundView.backgroundColor=[UIColor colorWithRed:0.220 green:0.227 blue:0.255 alpha:0.600];
            [self celeanBackGroundViewWithtag:3];
            if(_vcSide)
            {
                [_vcSide showContentViewWithModel:@"GroupBuy" withSb:@"SideLeft"];
            }
            
        }
            //Publish
            break;
        case 3:
        {
            if (self.isLogin == NO) {
                [self presentLoginViewController];
                return;
            }
            self.forthCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.forthCell.frame];
            self.forthCell.selectedBackgroundView.backgroundColor=[UIColor colorWithRed:0.220 green:0.227 blue:0.255 alpha:0.600];
            [self celeanBackGroundViewWithtag:4];
            if(_vcSide)
            {
                [_vcSide showContentViewWithModel:@"Publish" withSb:@"SideLeft"];
            }
            
        }
            
            break;
        case 4:
        {
            self.fivethCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.fivethCell.frame];
            self.fivethCell.selectedBackgroundView.backgroundColor=[UIColor colorWithRed:0.220 green:0.227 blue:0.255 alpha:0.600];
            [self celeanBackGroundViewWithtag:5];
            if(_vcSide)
            {
                [_vcSide showContentViewWithModel:@"Share" withSb:@"SideLeft"];
            }
            
        }
            
            break;
        case 5:
        {
            self.sixthCell.selectedBackgroundView=[[UIView alloc]initWithFrame:self.sixthCell.frame];
            self.sixthCell.selectedBackgroundView.backgroundColor=[UIColor colorWithRed:0.220 green:0.227 blue:0.255 alpha:0.600];
            [self celeanBackGroundViewWithtag:6];
            if(_vcSide)
            {
                [_vcSide showContentViewWithModel:@"More" withSb:@"SideLeft"];
            }
            
        }
            
            break;
            
        default:
            break;
    }
}

#pragma mark - IsJumpToLoginViewController
-(void)presentLoginViewController
{
    LoginCityStarViewViewController *loginCityStarVC = [[LoginCityStarViewViewController alloc]init];
    BaseNavController *baseloginNav = [[BaseNavController alloc] initWithRootViewController:loginCityStarVC];
    [self presentViewController:baseloginNav animated:YES completion:^{
    }];
}

@end
